package com.sumainfor.common.UnionPay;

import com.google.gson.Gson;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by zwj on 2019/1/18.
 */
public class DataEncodeUtil {
    private static String publickey = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQC4W7KA25AvgoMjwnxo+cdNhTASbEGQOQkgulOac1oS4YHL9eb8QFkx++CZffU21UqgIUpAo8qR1wf6/3j8TECnU147671rARINJWLl4Oj4KRInt5FTjBdHC6M6NBQBpQv6xFrtlq8UGORE9LS4HnPoKGoKiIyTPEO18ilsNkHDuwIDAQAB";
    private static String privatekey = "MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBALhbsoDbkC+CgyPCfGj5x02FMBJsQZA5CSC6U5pzWhLhgcv15vxAWTH74Jl99TbVSqAhSkCjypHXB/r/ePxMQKdTXjvrvWsBEg0lYuXg6PgpEie3kVOMF0cLozo0FAGlC/rEWu2WrxQY5ET0tLgec+goagqIjJM8Q7XyKWw2QcO7AgMBAAECgYBujibEmWak5wpHdoju1uMK/NTwJ1VF7L4pjzM1ivk4G12f903WdaxHijmNMnjLsiq3Tl9d1htTS/UwMuXVvtCBwWelrTxihNQzcNgS1NNps4fXS6RfhTfGLJLi2tb3AjMBaOgtGosl7GTcJwNewbJr4fv/ElMeH+YSc8Lw79MVOQJBAO/unjJNqjIN/dzcASuLZlV+g7q6GIZp1kqonvySqBJJJtMIpXi7+0uAuxY6id37qz7crqEzRyGuJj8pZnYaf28CQQDEtFLNlVpCkrD2eAfsQYYvxsTNGmFsgmhURO0i8uS4e4ey6JntDh6l75u+a1f6nugh1saoIYDfTPMK7yj979p1AkBRHQ6GWxNK0MgePpJ2si3qgVbvbbKU3nr/ynnVUY9YfzqM5cNrScHvCJo3LZsmXMrL+bdf8AIANOvhNpHZI6QpAkEAlxwYCErV/hKG07C+FWb42LcP9Khxc1RzJVmV+qUxw+9R/cTmis+wB7WcpZn9ClEM7wH5tquWPvT0ONAlY37hCQJAGQ1ITivxyRperaXnHrUcw2b874b0I9jaWpJqh2aGgeHX5KWRQdHJvtEPJVT4lWjXylm5jZb2AGqsgGVK2Bc/tA==";

    public static Map<String, String> encode(Map<String, Object> map) {
        EncryptManager encryptManager = new EncryptManager(privatekey,publickey);
        Map<String, String> reqParams = new HashMap<>();
        reqParams.put("mobKey",encryptManager.getMobKey());
        map.forEach((k,v)->{
            if(k.equals("data")){
                Gson gson = new Gson();
                String val = gson.toJson(v);
                reqParams.put(k,encryptManager.encryptStr(val));//对 data 加密
            }else{
                reqParams.put(k, (String) v);
            }
        });

        SignUtils.getSign(reqParams,privatekey);//加签
        return reqParams;
    }

    private static byte[] objectToByteArray(Object obj) {
        byte[] bytes = null;
        ByteArrayOutputStream byteArrayOutputStream = null;
        ObjectOutputStream objectOutputStream = null;

        try {
            byteArrayOutputStream = new ByteArrayOutputStream();
            objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);
            objectOutputStream.writeObject(obj);
            objectOutputStream.flush();
            bytes = byteArrayOutputStream.toByteArray();
        } catch (IOException var17) {
        } finally {
            if(objectOutputStream != null) {
                try {
                    objectOutputStream.close();
                } catch (IOException var16) {
                    var16.printStackTrace();
                }
            }

            if(byteArrayOutputStream != null) {
                try {
                    byteArrayOutputStream.close();
                } catch (IOException var15) {
                    var15.printStackTrace();
                }
            }

        }

        return bytes;
    }
}
